package no.ntnu.idi.lemma;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import no.ntnu.idi.deid.config.FilePaths;
import no.ntnu.idi.deid.documentunits.Token;
import no.ntnu.idi.deid.preprocessor.postagger.POSTag;

public class Norkompleks {
	
	private static Norkompleks instance = null;
	
	private Map<NorkompleksKey,NorkompleksGroup> norkompleksMap;
	
	protected Norkompleks() {
		norkompleksMap = new HashMap<NorkompleksKey,NorkompleksGroup>();
		generateNorkompleksMap();
	}
	
	public static Norkompleks getInstance() {
		      if(instance == null) {
		         instance = new Norkompleks();
		      }
		      return instance;
	}
	
	private void generateNorkompleksMap() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(new File(FilePaths.getNorkompleksFile())));
		String line;
		String lastId = "8722";//"Hack" index of first element
		String lastStem = "";
		POSTag lastpos = null;
		HashSet<String> temp = new HashSet<String>();
		ArrayList<NorkompleksKey> norKomptemp = new ArrayList<NorkompleksKey>();
		
		while ((line = br.readLine()) != null) {
			if(!line.isEmpty()) {
				String[] test = line.split("\\t");
				if(test.length == 4) {
					String word = test[0];
					POSTag postag = getPosTagFromString(test[1]);
					String stemWord = test[2];
					String id = test[3];
					if(!lastId.equals(id)) {
						NorkompleksGroup group = new NorkompleksGroup(lastStem, temp, lastpos);
						for(NorkompleksKey key: norKomptemp) {
							norkompleksMap.put(key, group);
						}

						temp = new HashSet<String>();
						norKomptemp = new ArrayList<NorkompleksKey>();
					}
					lastId = id; 
					lastStem = stemWord;
					lastpos = postag;
					norKomptemp.add(new NorkompleksKey(word, postag));
					temp.add(word);
				}
			}
		}
		br.close();	
		} catch (IOException e) {
			try {
				br.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			e.printStackTrace();
		}
	}
		
	private POSTag getPosTagFromString(String str) {
		String[] array = str.split(" ");
		String prefix = "pos=";
		for(String s: array){
			if(s.contains(prefix)){
				return getPosTag(s.substring(prefix.length()));
			}
		}
		return null;	
	}
	
	private POSTag getPosTag(String str){
		if(str.equals("subst")){
			return POSTag.NOUN; //!!!!!!! NOUN / NOUN_PROP !!!!
		}
		else if(str.equals("adv")){
			return POSTag.ADV;
		}
		else if(str.equals("prep")){
			return POSTag.PREP;
		}
		else if(str.equals("adj")){
			return POSTag.ADJ;
		}
		else if(str.equals("konj")){
			return POSTag.KONJ;
		}
		else if(str.equals("verb")){
			return POSTag.VERB;
		}
		else if(str.equals("det")){
			return POSTag.DET;
		}
		else if(str.equals("interj")){
			return POSTag.INTERJ;
		}
		else if(str.equals("subj")){
			return POSTag.SUBJ;
		}
		else if(str.equals("pron")){
			return POSTag.PRON;
		}
		else if(str.equals("infm")) {
			return POSTag.INFM;
		}
		else{
			return null;
		}		
	}
	
	public NorkompleksGroup search(String input, POSTag tag) {
		NorkompleksGroup result = norkompleksMap.get(new NorkompleksKey(input.toLowerCase(),tag));
		return result;
	}
	
	public NorkompleksGroup search(Token token) {
		return search(token.getStripped(),token.getPOSTag());
	}	
}
